package org.lq.ht.view.vip;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.StyledEditorKit.BoldAction;

import org.lq.ht.entity.Dictionary;
import org.lq.ht.entity.TypeID;
import org.lq.ht.entity.Vip;
import org.lq.ht.service.DictionaryService;
import org.lq.ht.service.VipService;
import org.lq.ht.service.impl.DictionaryServiceImpl;
import org.lq.ht.service.impl.VipServiceImpl;
import org.lq.ht.util.AutoNo;
import org.lq.ht.util.CastUtil;

import com.eltima.components.ui.DatePicker;
import com.mysql.fabric.xmlrpc.base.Data;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JPasswordField;
import javax.swing.JSpinner;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.DefaultBoundedRangeModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Font;

public class V_VipAdd extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JComboBox comboBox;
	private DatePicker dp;
	private JPasswordField passwordField_1;
	private JSpinner spinner;
	private JSpinner spinner_1;
	private JEditorPane editorPane;
	private JCheckBox chckbxNewCheckBox;
	//ViP编号
	private int vno=0;
	//新增状态
	private boolean status = false;
	private JCheckBox chckbxNewCheckBox_1;
	private static List<Dictionary> list ;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					V_VipAdd frame = new V_VipAdd();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public V_VipAdd(int vid) {
		this();
		VipService vs = new VipServiceImpl();
		Vip v = vs.getByVid(vid);
		vno=v.getNo();
		//		System.out.println(vno);
		textField.setText(CastUtil.castString(v.getVid()));
		textField_1.setText(CastUtil.castString(v.getName()));
		textField_2.setText(v.getPhone());
		textField_3.setText(v.getId_card());
		textField_4.setText(v.getAddress());
		passwordField.setText(CastUtil.castString(v.getPassword()));
		passwordField_1.setText(CastUtil.castString(v.getPassword()));
		spinner.setValue(CastUtil.castInt(v.getDiscount()));
		spinner_1.setValue(v.getIntegral());
		dp.getInnerTextField().setText(CastUtil.format(v.getHandlecarddate(), CastUtil.TIME_DATE_PATTERN));
		editorPane.setText(v.getNote());
		chckbxNewCheckBox_1.setEnabled(false);

		for (Dictionary d : list) {
			if(d.getNo()==v.getType()) {
				comboBox.setSelectedItem(d);
			}
		}


	}

	public V_VipAdd() {
		setTitle("Vip基本信息");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 323, 494);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("会员卡号");
		lblNewLabel.setBounds(32, 26, 54, 15);
		contentPane.add(lblNewLabel);

		textField = new JTextField();
		textField.setBounds(96, 23, 87, 21);
		contentPane.add(textField);
		textField.setColumns(10);

		chckbxNewCheckBox = new JCheckBox("自动");
		chckbxNewCheckBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(chckbxNewCheckBox.isSelected()) {
					VipService vs = new VipServiceImpl();
					List<Long> vid = vs.findAllVid();

					Boolean b = false;//没有相同的编号
					long vipNo = 0;
					do {
						b = false;//没有相同的编号
						vipNo = AutoNo.getVipNo();
						for (int i = 0; i < vid.size(); i++) {
							if( vipNo== vid.get(i)) {
								b = true; //有相同的编号
								break;
							}
//							System.out.println(vid.get(i));
						}
					}while(b);// 有相同的编号，循环

					textField.setText(CastUtil.castString(vipNo));
					textField.setEditable(false);
				}else {
					textField.setText("");
					textField.setEditable(true);
				}
			}
		});
		chckbxNewCheckBox.setBounds(187, 22, 103, 23);
		contentPane.add(chckbxNewCheckBox);

		JLabel lblNewLabel_1 = new JLabel("密    码");
		lblNewLabel_1.setBounds(32, 58, 54, 15);
		contentPane.add(lblNewLabel_1);

		passwordField = new JPasswordField();
		passwordField.setBounds(96, 54, 123, 21);
		contentPane.add(passwordField);

		JLabel lblNewLabel_2 = new JLabel("折扣");
		lblNewLabel_2.setBounds(180, 131, 32, 15);
		contentPane.add(lblNewLabel_2);

		spinner = new JSpinner();
		spinner.setBounds(210, 128, 60, 22);
		contentPane.add(spinner);

		JLabel lblNewLabel_3 = new JLabel("名    字");
		lblNewLabel_3.setBounds(32, 131, 54, 15);
		contentPane.add(lblNewLabel_3);

		textField_1 = new JTextField();
		textField_1.setBounds(96, 128, 74, 21);
		contentPane.add(textField_1);
		textField_1.setColumns(10);

		JLabel lblNewLabel_4 = new JLabel("手 机 号");
		lblNewLabel_4.setBounds(32, 166, 54, 15);
		contentPane.add(lblNewLabel_4);

		textField_2 = new JTextField();
		textField_2.setBounds(96, 163, 123, 21);
		contentPane.add(textField_2);
		textField_2.setColumns(10);

		JLabel lblNewLabel_5 = new JLabel("身份证号");
		lblNewLabel_5.setBounds(32, 197, 54, 15);
		contentPane.add(lblNewLabel_5);

		textField_3 = new JTextField();
		textField_3.setBounds(96, 194, 123, 21);
		contentPane.add(textField_3);
		textField_3.setColumns(10);

		JLabel lblNewLabel_6 = new JLabel("地    址");
		lblNewLabel_6.setBounds(32, 225, 54, 15);
		contentPane.add(lblNewLabel_6);

		textField_4 = new JTextField();
		textField_4.setBounds(96, 222, 123, 21);
		contentPane.add(textField_4);
		textField_4.setColumns(10);

		JLabel lblNewLabel_7 = new JLabel("会员类型");
		lblNewLabel_7.setBounds(32, 261, 54, 15);
		contentPane.add(lblNewLabel_7);

		comboBox = new JComboBox();
		comboBox.setBounds(96, 257, 81, 23);
		contentPane.add(comboBox);

		JLabel lblNewLabel_8 = new JLabel("积分");
		lblNewLabel_8.setBounds(187, 261, 32, 15);
		contentPane.add(lblNewLabel_8);

		spinner_1 = new JSpinner();
		spinner_1.setBounds(216, 258, 54, 22);
		contentPane.add(spinner_1);

		dp = new DatePicker();
		//		dp.getInnerButton().setEnabled(false);
		//		dp.getInnerTextField().setEnabled(false);
		dp.getInnerTextField().setText(CastUtil.format(new Date(), CastUtil.TIME_DATE_PATTERN));
		dp.setBounds(96, 296, 150, 21);
		contentPane.add(dp);

		JLabel lblNewLabel_9 = new JLabel("办卡日期");
		lblNewLabel_9.setBounds(32, 302, 54, 15);
		contentPane.add(lblNewLabel_9);

		JLabel lblNewLabel_10 = new JLabel("备    注");
		lblNewLabel_10.setBounds(32, 339, 54, 15);
		contentPane.add(lblNewLabel_10);

		editorPane = new JEditorPane();
		editorPane.setBounds(96, 339, 160, 50);
		contentPane.add(editorPane);

		JButton btnNewButton = new JButton("确定");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//保存
				VipService vs = new VipServiceImpl();

				if(noNull()) {
					Vip v = new Vip();
					v.setVid(CastUtil.castInt(textField.getText()));
					//				System.out.println(new String(passwordField.getPassword()));
					v.setPassword(new String(passwordField.getPassword()));
					v.setName(textField_1.getText());
					v.setDiscount(CastUtil.castString(spinner.getValue()));
					v.setPhone(textField_2.getText());
					v.setId_card(textField_3.getText());
					v.setAddress(textField_4.getText());
					Dictionary d = (Dictionary) comboBox.getSelectedItem();
					v.setType(d.getNo());
					v.setIntegral(CastUtil.castInt(spinner_1.getValue()));
					try {
						v.setHandlecarddate(new Timestamp(CastUtil.parse(dp.getInnerTextField().getText(), CastUtil.TIME_DATE_PATTERN).getTime()));
					} catch (ParseException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					v.setNote(editorPane.getText());
					if(vno==0) {
						if(vs.saveVip(v)) {
							JOptionPane.showMessageDialog(V_VipAdd.this, "添加成功！");
							if(status==true) {
								reset();
							}else {
								dispose();
							}
						}else {
							JOptionPane.showMessageDialog(V_VipAdd.this, "添加失败！");
						}
					}else {
						v.setNo(vno);
						if(vs.updateVip(v)) {
							JOptionPane.showMessageDialog(V_VipAdd.this, "修改成功！");
							dispose();
						}else {
							JOptionPane.showMessageDialog(V_VipAdd.this, "修改失败！");
						}
					}
				}


			}
		});
		btnNewButton.setBounds(60, 409, 93, 23);
		contentPane.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("重置");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//重置
				reset();
			}
		});
		btnNewButton_1.setBounds(177, 409, 93, 23);
		contentPane.add(btnNewButton_1);

		JLabel lblNewLabel_11 = new JLabel("再次密码");
		lblNewLabel_11.setBounds(32, 93, 54, 15);
		contentPane.add(lblNewLabel_11);

		passwordField_1 = new JPasswordField();
		passwordField_1.setBounds(96, 90, 123, 21);
		contentPane.add(passwordField_1);

		chckbxNewCheckBox_1 = new JCheckBox("新增");
		chckbxNewCheckBox_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//新增
				if(chckbxNewCheckBox_1.isSelected()) {
					status = true;
				}else {
					status = false;
				}
			}
		});
		chckbxNewCheckBox_1.setBounds(6, 426, 54, 23);
		contentPane.add(chckbxNewCheckBox_1);

		initcb();
	}

	public void initcb(){
		DictionaryService ds = new DictionaryServiceImpl();
		list = ds.findById(TypeID.会员级别);

		DefaultComboBoxModel<Dictionary> dcbm = new DefaultComboBoxModel<Dictionary>(list.toArray(new Dictionary[] {}));
		//		for (Dictionary dictionary : list) {
		//			System.out.println(dictionary +"\t" +dictionary.getNo());
		//		}
		comboBox.setModel(dcbm);
	}
	//判断数据是否为空
	public Boolean noNull() {
		if(textField.getText().equals("")) {
			JOptionPane.showMessageDialog(V_VipAdd.this, "卡号不能为空！");
		}else {
			VipService vs = new VipServiceImpl();
			List<Long> vid = vs.findAllVid();
			Boolean b =false;//无相同编号
			for (int i = 0; i < vid.size(); i++) {
				if(CastUtil.castInt(textField.getText()) == vid.get(i)) {
					b = true; //有相同的编号
					break;
				}
			}
			if(b) {
				JOptionPane.showMessageDialog(V_VipAdd.this, "已有编号！");
				textField.setText("");
			}else {
				if(Vid(textField.getText())) {
					if(new String(passwordField.getPassword()).equals("") || new String(passwordField_1.getPassword()).equals("")) {
						JOptionPane.showMessageDialog(V_VipAdd.this, "密码不能为空！");
					}else {
						if(new String(passwordField.getPassword()).equals(new String(passwordField_1.getPassword()))){
							if(textField_1.getText().equals("")) {
								JOptionPane.showMessageDialog(V_VipAdd.this, "名字不能为空！");
							}else {
								if(textField_2.getText().equals("")){
									JOptionPane.showMessageDialog(V_VipAdd.this, "手机号不能为空！");
								}else {
									if(phone(textField_2.getText())) {
										if(textField_3.getText().equals("")){
											JOptionPane.showMessageDialog(V_VipAdd.this, "身份证不能为空！");
										}else {
											if(textField_4.getText().equals("")) {
												JOptionPane.showMessageDialog(V_VipAdd.this, "地址不能为空！");
											}else {
												return true;
											}
										}
									}else {
										JOptionPane.showMessageDialog(V_VipAdd.this, "手机号格式不对！");
									}

								}
							}
						}else {
							JOptionPane.showMessageDialog(V_VipAdd.this, "两次密码不一样！");
						}
					}
				}else {
					JOptionPane.showMessageDialog(V_VipAdd.this, "编号为4位！");
				}
			}



		}
		return false;
	}

	//正则卡号
	public boolean Vid(String num) {
		String n = "\\d{4}";
		boolean b = Pattern.matches(n, num)  ? true :false;
		return b;
	}
	//正则手机号
	public boolean phone(String phone) {
		String p1 = "139\\d{8}";
		String p2 = "135\\d{8}";
		boolean b = Pattern.matches(p1, phone) | Pattern.matches(p2, phone) ? true :false;
		return b;
	}

	//重置
	public void reset() {
		textField.setEditable(true);
		chckbxNewCheckBox.setSelected(false);
		textField.setText("");
		textField_1.setText("");
		textField_2.setText("");
		textField_3.setText("");
		textField_4.setText("");
		passwordField.setText("");
		passwordField_1.setText("");
		spinner.setValue(0);
		spinner_1.setValue(0);
		dp.getInnerTextField().setText(CastUtil.format(new Date(), CastUtil.TIME_DATE_PATTERN));
		editorPane.setText("");
		initcb();
	}
}
